package com.axc.gallery2;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


/**
 * 主题应用设置类
 *
 */
public class ThemeChoice extends ListActivity {
	private List<Map<String, Object>> mData;
	//private int temp = -1; 
	//主题应用
	public static ThemeChoice Talbum;
	public int theme = -1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Talbum=this;
		Talbum=this;
		theme = getSharedPreferences("cons",MODE_PRIVATE).getInt("theme", 
				android.R.style.Theme); 
		setTheme(theme);
		//Utils.onActivityCreateSetTheme(this);   
		super.onCreate(savedInstanceState); 
		mData = getData(); 
		MyAdapter adapter = new MyAdapter(this);
		setListAdapter(adapter);
	}
	private List<Map<String, Object>> getData() {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("textContent", "蓝色主题");
		map.put("themePic", "#3771A0");
		list.add(map);

		map = new HashMap<String, Object>();
		map.put("textContent", "白色主题");
		map.put("themePic", "#C9C9C9");
		list.add(map);

		map = new HashMap<String, Object>();
		map.put("textContent", "橙色主题"); 
		map.put("themePic", "#e89a03");
		list.add(map);
		
		map = new HashMap<String, Object>();
		map.put("textContent", "粉红色主题"); 
		map.put("themePic", "#EF20A8");
		list.add(map);
		
		return list;
	}  
    
    public final class ViewHolder{
		public TextView text;
		//public RadioButton radioBtn;
		public RelativeLayout reLayout;
		public ImageView themeImg;
	}
    
  //侦听用户的点击返回键,提示程序退出
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    // TODO Auto-generated method stub
		Intent intent = new Intent();
		intent.setClass(ThemeChoice.this, IndexActivity.class);
		startActivity(intent); 
		this.finish();
	    return false;
	} 
	public class MyAdapter extends BaseAdapter{
		
		private LayoutInflater mInflater;
		
		
		public MyAdapter(Context context){
			this.mInflater = LayoutInflater.from(context);
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mData.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			final int c=position;
			//final String tString=(String)mData.get(position).get("textContent");
			ViewHolder holder = null;
			if (convertView == null) {
				
				holder=new ViewHolder();  
				
				convertView = mInflater.inflate(R.layout.theme_item_schema, null);
				//holder.radioBtn = (RadioButton)convertView.findViewById(R.id.radioChoice);
				holder.text=(TextView)convertView.findViewById(R.id.textContent);
				holder.reLayout=(RelativeLayout)convertView.findViewById(R.id.themeCon);
				holder.themeImg=(ImageView)convertView.findViewById(R.id.themePic);
				convertView.setTag(holder);
				
			}else {
				holder = (ViewHolder)convertView.getTag();
			} 
			holder.text.setText((String)mData.get(position).get("textContent"));
			holder.text.setTextColor(Color.parseColor((String.valueOf(mData.get(position).get("themePic")))));
			holder.themeImg.setBackgroundColor(Color.parseColor((String.valueOf(mData.get(position).get("themePic")))));
			
			
			//RadioButton单选设置
			/*holder.radioBtn.setId(position);
			holder.radioBtn.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					if (isChecked) {

						if (temp != -1) {

						RadioButton tempButton = (RadioButton)findViewById(temp);

						if (tempButton != null) {

						tempButton.setChecked(false);

						}

						}

						temp = buttonView.getId();

						}
				}
			});
			holder.radioBtn.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					if(c==0){  
					    Toast.makeText(ThemeChoice.this, "第"+String.valueOf(c)+"条", Toast.LENGTH_SHORT).show();  
				    }
					else if (c==1) {
						Toast.makeText(ThemeChoice.this, "第"+String.valueOf(c)+"条", Toast.LENGTH_SHORT).show();  
					}
					else if (c==2) { 
						Toast.makeText(ThemeChoice.this, "第"+String.valueOf(c)+"条", Toast.LENGTH_SHORT).show();  
					}
				}
			});
			
			   if (position == temp) {

				holder.radioBtn.setChecked(true);

				} else {

				holder.radioBtn.setChecked(false);

				} 
			
			*/
			holder.text.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) { 
					if(c==0){
						Talbum.getSharedPreferences("cons",ListActivity.MODE_PRIVATE).edit()	
						.putInt("theme", R.style.BlueTheme).commit();
						Utils.changeToTheme(ThemeChoice.this, 1);
	                }
					if(c==1){
						Talbum.getSharedPreferences("cons",ListActivity.MODE_PRIVATE).edit()	
						.putInt("theme", R.style.WhiteTheme).commit();
						Utils.changeToTheme(ThemeChoice.this, 2); 
	                }
					if (c==2) {
						Talbum.getSharedPreferences("cons",ListActivity.MODE_PRIVATE).edit()	
						.putInt("theme", R.style.OrangeTheme).commit();
						Utils.changeToTheme(ThemeChoice.this, 3); 
					}
					if(c==3){
						Talbum.getSharedPreferences("cons",ListActivity.MODE_PRIVATE).edit()	
						.putInt("theme", R.style.PinkTheme).commit();
						Utils.changeToTheme(ThemeChoice.this, 4); 
					}
				}
			});
			holder.reLayout.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					if(c==0){
						Talbum.getSharedPreferences("cons",ListActivity.MODE_PRIVATE).edit()	
						.putInt("theme", R.style.BlueTheme).commit();
						Utils.changeToTheme(ThemeChoice.this, 1);
	                }
					if(c==1){
						Talbum.getSharedPreferences("cons",ListActivity.MODE_PRIVATE).edit()	
						.putInt("theme", R.style.WhiteTheme).commit();
						Utils.changeToTheme(ThemeChoice.this, 2); 
	                }
					if (c==2) {
						Talbum.getSharedPreferences("cons",ListActivity.MODE_PRIVATE).edit()	
						.putInt("theme", R.style.OrangeTheme).commit();
						Utils.changeToTheme(ThemeChoice.this, 3); 
					}
					if(c==3){
						Talbum.getSharedPreferences("cons",ListActivity.MODE_PRIVATE).edit()	
						.putInt("theme", R.style.PinkTheme).commit();
						Utils.changeToTheme(ThemeChoice.this, 4); 
					}
				}
			});
			return convertView;
		}
		
	} 
}